﻿namespace AC.ExtendedRenderer.Toolkit
{
    using AC.ExtendedRenderer.Toolkit.Drawing;
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(Panel))]
    public class KryptonPanelEx : Panel
    {
        private Color _borderColor = Color.Gray;
        private int _borderWidth = 1;
        private LinearGradientMode _gradientDirection = LinearGradientMode.Vertical;
        private Color _gradientEndColor = Color.Gray;
        private Color _gradientStartColor = Color.White;
        private bool _gradientToogleColors;
        private bool _gradientUseBlend;
        private System.Drawing.Image _image;
        private Point _imageLocation = new Point(4, 4);
        private static IPalette _palette;
        private static PaletteRedirect _paletteRedirect;
        private bool _persistentColors;
        private int _roundCornerRadius = 1;
        private int _shadowOffSet;

        public KryptonPanelEx()
        {
            base.SetStyle(ControlStyles.DoubleBuffer, true);
            base.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            base.SetStyle(ControlStyles.ResizeRedraw, true);
            base.SetStyle(ControlStyles.UserPaint, true);
            base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            base.UpdateStyles();
            if (_palette != null)
            {
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            KryptonManager.GlobalPaletteChanged += new System.EventHandler(this.OnGlobalPaletteChanged);
            _palette = KryptonManager.CurrentGlobalPalette;
            _paletteRedirect = new PaletteRedirect(_palette);
            this.InitColors();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (_palette != null)
                {
                    _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                    _palette = null;
                }
                KryptonManager.GlobalPaletteChanged -= new System.EventHandler(this.OnGlobalPaletteChanged);
            }
            base.Dispose(disposing);
        }

        public static GraphicsPath GetRoundPath(Rectangle r, int depth)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddArc(r.X, r.Y, depth, depth, 180f, 90f);
            path.AddArc((r.X + r.Width) - depth, r.Y, depth, depth, 270f, 90f);
            path.AddArc((r.X + r.Width) - depth, (r.Y + r.Height) - depth, depth, depth, 0f, 90f);
            path.AddArc(r.X, (r.Y + r.Height) - depth, depth, depth, 90f, 90f);
            path.AddLine(r.X, (r.Y + r.Height) - depth, r.X, r.Y + (depth / 2));
            return path;
        }

        private void InitColors()
        {
            this._gradientStartColor = _palette.ColorTable.StatusStripGradientBegin;
            this._gradientEndColor = _palette.ColorTable.StatusStripGradientEnd;
            this._borderColor = _palette.ColorTable.ToolStripGradientEnd;
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (_palette != null)
            {
                _palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            _palette = KryptonManager.CurrentGlobalPalette;
            _paletteRedirect.Target = _palette;
            if (_palette != null)
            {
                _palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                if (!this._persistentColors)
                {
                    this.InitColors();
                }
            }
            base.Invalidate();
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            Color color;
            Color color2;
            base.OnPaintBackground(e);
            int x = Math.Min(Math.Min(this._shadowOffSet, base.Width - 2), base.Height - 2);
            int depth = Math.Min(Math.Min(this._roundCornerRadius, base.Width - 2), base.Height - 2);
            if ((base.Width <= 1) || (base.Height <= 1))
            {
                return;
            }
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            Rectangle r = new Rectangle(0, 0, (base.Width - x) - 1, (base.Height - x) - 1);
            Rectangle rectangle2 = new Rectangle(x, x, (base.Width - x) - 1, (base.Height - x) - 1);
            GraphicsPath roundPath = GetRoundPath(rectangle2, depth);
            GraphicsPath path = GetRoundPath(r, depth);
            if (depth > 0)
            {
                using (PathGradientBrush brush = new PathGradientBrush(roundPath))
                {
                    brush.WrapMode = WrapMode.Clamp;
                    ColorBlend blend = new ColorBlend(3);
                    blend.Colors = new Color[] { Color.Transparent, Color.FromArgb(180, Color.DimGray), Color.FromArgb(180, Color.DimGray) };
                    float[] numArray = new float[3];
                    numArray[1] = 0.1f;
                    numArray[2] = 1f;
                    blend.Positions = numArray;
                    brush.InterpolationColors = blend;
                    e.Graphics.FillPath(brush, roundPath);
                }
            }
            if (!this._gradientToogleColors)
            {
                color = this._gradientStartColor;
                color2 = this._gradientEndColor;
            }
            else
            {
                color = this._gradientEndColor;
                color2 = this._gradientStartColor;
            }
            if (!this._gradientUseBlend)
            {
                using (LinearGradientBrush brush2 = new LinearGradientBrush(r, color, color2, this.GradientDirection))
                {
                    e.Graphics.FillPath(brush2, path);
                    goto Label_01FE;
                }
            }
            DrawingMethods.DrawGradient(e.Graphics, r, color, color2, 90f, false, Color.White, 0f);
        Label_01FE:
            if (this._borderWidth > 0)
            {
                e.Graphics.DrawPath(new Pen(Color.FromArgb(180, this._borderColor), (float) this._borderWidth), path);
            }
            if (this._image != null)
            {
                e.Graphics.DrawImageUnscaled(this._image, this._imageLocation);
            }
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(true), Category("Appearance-Extended"), DefaultValue("Color.Gray")]
        public Color BorderColor
        {
            get
            {
                return this._borderColor;
            }
            set
            {
                this._borderColor = value;
                base.Invalidate();
            }
        }

        [Browsable(true), DefaultValue(1), Category("Appearance-Extended")]
        public int BorderWidth
        {
            get
            {
                return this._borderWidth;
            }
            set
            {
                this._borderWidth = value;
                base.Invalidate();
            }
        }

        [Category("Appearance-Extended"), DefaultValue("Vertical"), Browsable(true)]
        public LinearGradientMode GradientDirection
        {
            get
            {
                return this._gradientDirection;
            }
            set
            {
                this._gradientDirection = value;
                base.Invalidate();
            }
        }

        [Category("Appearance-Extended"), Browsable(true), DefaultValue("Color.Gray"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public Color GradientEndColor
        {
            get
            {
                return this._gradientEndColor;
            }
            set
            {
                this._gradientEndColor = value;
                base.Invalidate();
            }
        }

        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(true), Category("Appearance-Extended"), DefaultValue("Color.White")]
        public Color GradientStartColor
        {
            get
            {
                return this._gradientStartColor;
            }
            set
            {
                this._gradientStartColor = value;
                base.Invalidate();
            }
        }

        [Category("Appearance-Extended"), DefaultValue("False"), Browsable(true)]
        public bool GradientToogleColors
        {
            get
            {
                return this._gradientToogleColors;
            }
            set
            {
                this._gradientToogleColors = value;
            }
        }

        [Browsable(true), DefaultValue("False"), Category("Appearance-Extended")]
        public bool GradientUseBlend
        {
            get
            {
                return this._gradientUseBlend;
            }
            set
            {
                this._gradientUseBlend = value;
            }
        }

        [Category("Appearance-Extended"), Browsable(true)]
        public System.Drawing.Image Image
        {
            get
            {
                return this._image;
            }
            set
            {
                this._image = value;
                base.Invalidate();
            }
        }

        [Browsable(true), DefaultValue("4,4"), Category("Appearance-Extended")]
        public Point ImageLocation
        {
            get
            {
                return this._imageLocation;
            }
            set
            {
                this._imageLocation = value;
                base.Invalidate();
            }
        }

        [DefaultValue("False"), Category("Appearance-Extended"), Browsable(true)]
        public bool PersistentColors
        {
            get
            {
                return this._persistentColors;
            }
            set
            {
                this._persistentColors = value;
            }
        }

        [Category("Appearance-Extended"), DefaultValue(1), Browsable(true)]
        public int RoundCornerRadius
        {
            get
            {
                return this._roundCornerRadius;
            }
            set
            {
                if (value <= 0)
                {
                    this._roundCornerRadius = 1;
                }
                else
                {
                    this._roundCornerRadius = Math.Abs(value);
                }
                base.Invalidate();
            }
        }

        [Browsable(true), DefaultValue(0), Category("Appearance-Extended")]
        public int ShadowOffSet
        {
            get
            {
                return this._shadowOffSet;
            }
            set
            {
                this._shadowOffSet = Math.Abs(value);
                base.Invalidate();
            }
        }
    }
}

